package com.my.study.http;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;

import java.net.URI;

/**
 * {@link SimpleChannelInboundHandler}是{@link ChannelInboundHandlerAdapter}子类
 * 客户端和服务端相互通讯的数据被封装为{@link HttpObject}
 *
 * @author: yidujun
 * @create: 2021/04/12 14:44
 */
public class HttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {

    private static final String FAVICON_ICO = "/favicon.ico";


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
        Channel channel = ctx.channel();
        ChannelPipeline pipeline = ctx.pipeline();
        Channel channelFromPipeline = pipeline.channel();
        ChannelHandler handler = ctx.handler();

        System.out.println("对应的channel：" + channel + "， pipeline：" + pipeline);
        System.out.println("通过pipeline获取channel：" + channelFromPipeline + "， handler：" + handler);

        //判断 msg 是不是 httpRequest请求
        if (msg instanceof HttpRequest) {
            System.out.println("crx 类型：" + ctx.getClass());
            System.out.println("pipeline hashcode" + pipeline.hashCode() + ", HttpServerHandler hash=" + this.hashCode());

            System.out.println("msg 类型=" + msg.getClass());
            System.out.println("客户端地址" + channel.remoteAddress());

            HttpRequest httpRequest = (HttpRequest) msg;
            URI uri = new URI(httpRequest.uri());

            if (FAVICON_ICO.equals(uri.getPath())) {
                System.out.println("请求了 favicon.ico, 不做响应");
                return;
            }

            //回复信息给浏览器 [http协议]
            ByteBuf content = Unpooled.copiedBuffer("hello, 我是服务器", CharsetUtil.UTF_8);
            //构造一个http的响应，即 httpResponse
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);

            // 尝试使用两种set方法构建请求头信息
            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
            HttpHeaders headers = new DefaultHttpHeaders();
            headers.add(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
            response.headers().set(headers);

            //将构建好的response返回
            ctx.writeAndFlush(response);
        }
    }
}
